//
//  LoginViewController.m
//  VEW
//
//  Created by lanou3g on 15/7/18.
//  Copyright (c) 2015年 Y团. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"

#import "User.h"
#import "LTView.h"
#import "RegistViewController.h"
//#import "LeftSlideViewController.h"

@interface LoginViewController ()

@property (nonatomic,retain) LoginView * loginView;//根视图

@end

@implementation LoginViewController

- (void)dealloc
{
    Block_release(_successBlock);
    self.loginView = nil;
    [super dealloc];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)loadView
{
    self.loginView = [[LoginView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.view = _loginView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"用户登陆";
    
    //导航栏的返回按钮
    UIImage *leftButtonImage=[UIImage imageNamed:@"返回"];
    
    leftButtonImage=[leftButtonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithImage:leftButtonImage style:UIBarButtonItemStyleDone target:self action:@selector(doback)];

    
    //登陆页面的登陆按钮添加响应方法
    [_loginView.loginButton addTarget:self action:@selector(didClickLoginButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //登陆页面的注册按钮添加响应方法
    [_loginView.registButton addTarget:self action:@selector(didClickRegistButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
//    //当从注册页面完成注册返回登陆页面时，根据用户的登陆状态，进行登陆成功回调，并退出登陆页面
//    BOOL isLogin = [[FileHandle shareInstance] loginState];
//    
//    if (YES == isLogin) {
//        
//        //登陆成功后回调，同时将用户信息回传
//        User * user = [[FileHandle shareInstance] user];
//        self.successBlock(user);
//        
//        //推出登陆页面
//        [self dismissViewControllerAnimated:YES completion:nil];
//    }
    
}


# pragma mark ------控制方法------
//返回
-(void)doback
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didClickRegistButtonAction:(UIButton *)button
{
    //进入注册页面
    RegistViewController * registVC = [[RegistViewController alloc] init];
    [self.navigationController pushViewController:registVC animated:YES];
}

//点击登陆按钮，验证用户登陆
- (void)didClickLoginButtonAction:(UIButton *)button
{
//    //从沙盒中获取用户名和密码
//    NSString * username = [[FileHandle shareInstance] username];
//    NSString * password = [[FileHandle shareInstance] password];
//    
//    //验证登陆
//    if ([[_loginView.usernameView inputFieldText] isEqualToString:username] && [[_loginView.passwordView inputFieldText] isEqualToString:password]) {
//        
//        //验证登陆成功，设置用户为登陆状态
//        [[FileHandle shareInstance] setloginState:YES];
//        [[FileHandle shareInstance] synchronize];
//        
//        //登陆成功后回调，同时将用户信息回传
//        User * user = [[FileHandle shareInstance] user];
//        self.successBlock(user);
//        
//        //推出登陆页面
//        [self dismissViewControllerAnimated:YES completion:nil];
    
        
//    }else{
//        
//        //登陆失败，显示提示框，停留在登陆页面
//        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名或密码错误！" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
//        [alertView show];
//    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
